package com.example.indenture.team.dao;


import com.example.indenture.common.vo.Search;
import com.example.indenture.team.entity.Team;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface TeamDao {
    //新增队伍
    @Insert("insert into team (create_date,update_time,lid,name,pass,rid,url,smid,vercode)" +
            "values(#{createDate},#{updateTime},#{lid},#{name},#{pass},#{rid},#{url},#{smid},#{vercode})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void insertTeam(Team team);

    //删除队伍
    @Delete("DELETE FROM team WHERE id= #{tid}")
    void deleteTeam(Integer tid);

    @Delete("DELETE FROM team_user WHERE tid= #{tid}")
    void deleteTeamUser(Integer tid);

    //更新队伍
    @Update("update team set create_date = #{createDate},update_time = #{updateTime},lid = #{lid},name = #{name},pass=#{pass},rid=#{rid}," +
            "url=#{url},smid=#{smid},vercode=#{vercode} where id = #{id}")
    void updateTeam(Team team);

    //根据队伍名查队伍
    @Select("select * from team where name = #{teamName} limit 1")
    Team getTeamByName(String teamName);

    //根据比赛id和队伍名字查找队伍
    @Select("<script>"
            + "select * from team"
            + "<where> "
            + "rid = #{rid}"
            + "<if test='search.keyword != \"\" and search.keyword != null'>"
            + " and (name like '%${search.keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='search.sort != \"\" and search.sort != null'>"
            + " Order by ${search.sort} ${search.direction}"
            + "</when>"
            + "<otherwise>"
            + " Order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<Team> teamByRaceAndName(Integer rid, Search search);

    //判断一个人在不在队伍中
    @Select("select id from race where id in (select rid from team where id in (select tid from team_user where uid =#{uid}))")
    List<Integer> isInTeam(Integer uid, Integer rid);

    //队员加入队伍
    @Insert("insert into team_user (uid,tid) values (#{uid},#{tid})")
    void addTeamUser(Integer uid, Integer tid, Integer rid);

    //退出队伍
    @Delete("DELETE FROM team_user WHERE tid=#{tid} and uid = #{uid}")
    void quitTeam(Integer tid, Integer uid);

    //查询一个队伍的人数
    @Select("SELECT COUNT(*) FROM team_user WHERE tid = #{tid}")
    int getTeamUserNum(Integer tid);

    //查询user在参加了哪些队伍
    @Select("select * from team where id in (select tid from team_user where uid =#{uid})")
    List<Team> teamListByUser(Integer uid);

    @Select("select vercode from team where id=#{tid}")
    Team getVercodeById(Integer tid);

    @Select("select * from team where vercode=#{vercode}")
    Team getTeamByVercode(Integer vercode);


    @Select("select vercode from team where id=#{tid}")
    Team ok(Integer vercode,Integer tid);

    @Select("select * from team where id=#{id}")
    Team findById(Integer id);


}
